﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizCountryCity : CustBizObject
    {
        private CDmDjkCountryCity m_CDmDjkCountryCity = new CDmDjkCountryCity();
        private readonly List<CDmDjkCountryCity> _allAreaInfoList = null;
        private CDmDjkCountryCity oDmDjkCountryCity
        {
            get { return m_CDmDjkCountryCity; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizCountryCity()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkCountryCity.DJK_COUNTRY_CITY);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["COUNTRY_CITY_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        //public DataSet SelectByIds(string strIdS)
        //{
        //    CDbParmCollection parmCollection = new CDbParmCollection();
        //    parmCollection.Add(new CDbParm("strIdS", ParameterDirection.Input, EDbType.String, 36, strIdS.Replace("'", "").Replace(",", "','")));
        //    DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('@strIdS')");
        //    ds.Tables[0].TableName = this.TableName;
        //    return ds;
        //}

        public DataSet SelectCountryByParentId(string parentId)
        {
            DataSet ds = null;
            if (string.IsNullOrEmpty(parentId))
            {
                return ds;
            }

            Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
            parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("ParentID", ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, parentId));

            ds = this.DataObj.QueryBySQL("SELECT COUNTRY_CITY_ID,COUNTRY_CITY_NME FROM DJK_COUNTRY_CITY WHERE PARENTID_FOR_CITY=@ParentID AND CITY_IS_DELETED=0 AND CITY_IS_DISPLAY=1 order by COUNTRY_CITY_SRT asc", parmCollection);
            return ds;
        }


        public void SetDefaultAreaInfo(string countryId, string cityId, string destinationId)
        {
            string path = System.Web.HttpContext.Current.Server.MapPath("/config/SysCustConfig.xml");

            var rssXDoc = XDocument.Load(path);

            rssXDoc.Element("areaInfo").SetElementValue("ButiqueCountry", countryId);
            rssXDoc.Element("areaInfo").SetElementValue("ButiqueCity", cityId);
            rssXDoc.Element("areaInfo").SetElementValue("ButiqueDestin", destinationId);

            rssXDoc.Save(path);
        }

        public string GetDefaultAreaInfo(string elementName)
        {
            string resultStr = string.Empty;
            string path = System.Web.HttpContext.Current.Server.MapPath("/config/SysCustConfig.xml");

            var rssXDoc = XDocument.Load(path);

            try
            {
                resultStr = rssXDoc.Element("areaInfo").Element(elementName).Value;
            }
            catch (Exception)
            {

            }
            return resultStr;

            //DefaultCity = rssXDoc.Element("areaInfo").Element("ButiqueCity").Value;
            //DefaultDestination = rssXDoc.Element("areaInfo").Element("ButiqueDestin").Value;
        }


        /// <summary>
        /// 查询热门城市
        /// </summary>
        /// <param name="parentId">城市父级（不传查询所有热门城市）</param>
        /// <returns>热门城市   </returns>
        ///  Author:任延华
        ///  Creation date:2015-9-28
        public DataSet GetTopCity(string parentId="")
        {
            DataSet ds = null;
            if (string.IsNullOrEmpty(parentId))
            {
                ds = this.DataObj.QueryBySQL("SELECT COUNTRY_CITY_ID,COUNTRY_CITY_NME,CITY_CARD FROM DJK_COUNTRY_CITY WHERE CITY_IS_DELETED=0 AND CITY_IS_DISPLAY=1 AND CITY_IS_TOP=1 order by COUNTRY_CITY_SRT asc");
            }
            else
            {
                Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("ParentID", ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, parentId));

                ds = this.DataObj.QueryBySQL("SELECT COUNTRY_CITY_ID,COUNTRY_CITY_NME,CITY_CARD FROM DJK_COUNTRY_CITY WHERE PARENTID_FOR_CITY=@ParentID AND CITY_IS_DELETED=0 AND CITY_IS_DISPLAY=1 AND CITY_IS_TOP=1 order by COUNTRY_CITY_SRT asc", parmCollection);
            }
            return ds;
        }

    }
}
